﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Word = Microsoft.Office.Interop.Word;
using Maticsoft.Common;

namespace WEB.knowledge
{
    public partial class knowledgeUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            int n = ddlType.SelectedIndex;
            if (n == 5)
            {
                ddlType0.Visible = false;
                Label1.Visible = false;
                Label2.Text = "只能上传zip或rar文件";
            }
            else
            {
                ddlType0.Visible = true;
                Label1.Visible = true;
                Label2.Text = "只能上传word文档、txt文档或pdf文档";
            }

        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (knowFileUpload.HasFile)
            {
                int n = ddlType.SelectedIndex;
                int i = ddlType0.SelectedIndex;
                string subFile = "";
                switch (n)
                {
                    case 0:
                        subFile = "rule";
                        break;
                    case 1:
                        subFile = "business";
                        break;
                    case 2:
                        subFile = "case";
                        break;
                    case 3:
                        subFile = "notice";
                        break;
                    case 4:
                        subFile = "port";
                        break;
                    case 5:
                        subFile = "compress";
                        break;
                }
                /*通过文件扩展名判断文件类型*/
                string fileExt = System.IO.Path.GetExtension(knowFileUpload.FileName);
                if (fileExt != ".doc" && fileExt != ".txt" && fileExt != ".pdf" && fileExt != ".zip" && fileExt != ".rar")
                {
                    msg += "文件类型错误！应为：doc、txt或pdf";
                }
                /*限制文件大小*/
                if (knowFileUpload.PostedFile.ContentLength > 100 * 1024 * 1024)
                {
                    msg += "文件应小于100MB";
                }
                if (msg != "")  //没有任何不妥
                {
                    MessageBox.ShowAndRedirect(this, msg,Request.Url.LocalPath.ToString());
                    return;
                }
                if (n != 5)
                {
                    if (i == 0)
                    {
                        string localPath = Server.MapPath("~/document/" + subFile + "/");
                        string fullPath = localPath + "(内部)" + knowFileUpload.FileName;
                        fullPath = Server.HtmlEncode(fullPath);
                        knowFileUpload.SaveAs(fullPath);
                        string ex = System.IO.Path.GetExtension(fullPath);
                        if (ex == ".doc" || ex == ".docx")  //如果是WORD文档，需要转换
                        {
                            string saveFileName = WordToHtml(fullPath);
                            //System.IO.File.Delete(fullPath);
                        }
                    }
                    else if (i == 1)
                    {
                        string localPath = Server.MapPath("~/document/" + subFile + "/");
                        string fullPath = localPath + "(公共)" + knowFileUpload.FileName;
                        fullPath = Server.HtmlEncode(fullPath);
                        knowFileUpload.SaveAs(fullPath);
                        string ex = System.IO.Path.GetExtension(fullPath);
                        if (ex == ".doc" || ex == ".docx")  //如果是WORD文档，需要转换
                        {
                            string saveFileName = WordToHtml(fullPath);
                            //System.IO.File.Delete(fullPath);
                        }
                    }
                    else if (i == 2)
                    {
                        string localPath = Server.MapPath("~/document/" + subFile + "/");
                        string fullPath = localPath + "(外部)" + knowFileUpload.FileName;
                        fullPath = Server.HtmlEncode(fullPath);
                        knowFileUpload.SaveAs(fullPath);
                        string ex = System.IO.Path.GetExtension(fullPath);
                        if (ex == ".doc" || ex == ".docx")  //如果是WORD文档，需要转换
                        {
                            string saveFileName = WordToHtml(fullPath);
                            //System.IO.File.Delete(fullPath);
                        }
                    }
                }
                else
                {
                    string localPath = Server.MapPath("~/document/" + subFile + "/");
                    string fullPath = localPath + knowFileUpload.FileName;
                    fullPath = Server.HtmlEncode(fullPath);
                    knowFileUpload.SaveAs(fullPath);
                }
                msg = "文件上传成功！";
            }
            else
            {
                msg = "未选择上传文件";
            }
            Response.Write("<script>alert(' " + msg +" ')</script>");
        }


        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName"></param>
        private string WordToHtml(object wordFileName)
        {

            //在此处放置用户代码以初始化页面
            Word.ApplicationClass word = new Word.ApplicationClass();
            Type wordType = word.GetType();

            Word.Documents docs = word.Documents;

            //打开文件
            Type docsType = docs.GetType();
            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });

            //转换格式，另存为
            Type docType = doc.GetType();

            string wordSaveFileName = wordFileName.ToString();

            string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";

            object saveFileName = (object)strSaveFileName;

            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });

            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);

            //退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

            return saveFileName.ToString();
        }

    }
}
